package com.wind.taobao.api.jianghu.convert
{
	import com.wind.taobao.api.jianghu.model.Icon;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Icon 转换器
	 * @author fxy
	 *
	 */
	public class IconConvert
	{
		/**
		 * 转换Icon集合
		 */
		public static function convertIcons(value:Object, type:String="json"):ArrayCollection
		{
			var icons:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							icons.addItem(convertIcon((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return icons;
		}

		/**
		 * 转换Icon
		 */
		public static function convertIcon(value:Object, type:String="json"):Icon
		{
			var icon:Icon=new Icon();
			if (value == null)
				return icon;
			if (type == TaobaoConstants.XML)
			{
				icon=convertXMLToIcon(XML(value), icon);
			}
			else
			{
				icon=convertJSONToIcon(value, icon);
			}
			return icon;
		}

		/**
		 * JSON转换为Icon
		 */
		private static function convertJSONToIcon(json:Object, icon:Icon):Icon
		{
			if (json.hasOwnProperty(TaobaoConstants.ICON_24))
			{
				icon.icon_24=json[TaobaoConstants.ICON_24];
			}
			if (json.hasOwnProperty(TaobaoConstants.ICON_40))
			{
				icon.icon_40=json[TaobaoConstants.ICON_40];
			}
			if (json.hasOwnProperty(TaobaoConstants.ICON_60))
			{
				icon.icon_60=json[TaobaoConstants.ICON_60];
			}
			if (json.hasOwnProperty(TaobaoConstants.ICON_120))
			{
				icon.icon_120=json[TaobaoConstants.ICON_120];
			}
			return icon;
		}

		/**
		 * XML转换为Icon
		 */
		private static function convertXMLToIcon(value:XML, icon:Icon):Icon
		{
			return icon;
		}

	}
}